from typing import Dict, Callable, Any
import inspect

class PluginBase:
    """插件基类定义:get_tools & get_functions""" 
    @classmethod
    def get_tools(cls) -> Dict[str, Dict]:
        """获取工具描述字典"""
        tools = {}
        for name, func in inspect.getmembers(cls, inspect.isfunction):
            if hasattr(func, '__tool_meta__'):
                tools[name] = func.__tool_meta__
        return tools

    @classmethod
    def get_functions(cls) -> Dict[str, Callable]:
        """获取函数映射"""
        return {name: func for name, func in inspect.getmembers(cls, inspect.isfunction) 
                if hasattr(func, '__tool_meta__')}

def tool(description: str, **param_decs):
    """函数装饰器，用于标记工具函数"""
    def decorator(func):
        # 自动生成参数schema
        params = {}
        param_list = []
        sig = inspect.signature(func)
        for name, param in sig.parameters.items():
            if name == 'self':
                continue
            param_list.append(name)
            param_type = param.annotation.__name__ if param.annotation!= inspect.Parameter.empty else "Any"
            if param_type == "str":
                param_type = "string"
            params[name] = {
                "type": param_type,
                "description": param_decs.get(name, "")
            }
        func.__tool_meta__ = {
            "description": description,
            "parameters": params,
            "required": param_list
        }
        return func
    return decorator

'''

"required": ["a","b"],

def tool(description: str, **param_decs):
    """函数装饰器，用于标记工具函数"""
    def decorator(func):
        # 自动生成参数schema
        params = {}
        sig = inspect.signature(func)
        for name, param in sig.parameters.items():
            if name == 'self':
                continue
            params[name] = {
                "type": param.annotation.__name__ if param.annotation != inspect.Parameter.empty else "Any",
                "description": param_decs.get(name, "")
            }
        func.__tool_meta__ = {
            "description": description,
            "parameters": params
        }
        return func
    return decorator
'''